const {Article} = require('../../model/article');
const path = require('path');
const formidable = require('formidable');
module.exports = (req,res)=>{
  //创建表单解析对象
  const form = new formidable.IncomingForm();
  //配置上传文件的存放位置
  form.uploadDir = path.join(__dirname,'../','../','public','uploads');
  //保留上传文件的后缀
  form.keepExtensions = true;
  //解析表单
  form.parse(req,async(err,fields,files)=>{
    //err错误对象  fields对象类型保存普通表单数据  files对象类型，保存了和上传文件相关的数据
    await Article.create({
      title:fields.title,
      author:fields.author,
      publishDate:fields.publishDate,
      cover:files.cover.path.split('public')[1],
      content:fields.content,
    })
    res.redirect('/admin/article')
  })
}